<template>
  <div class="login-page">
    <el-card class="login-card">
      <h2 style="text-align: center;">管理员登录</h2>
      <el-form :model="form" ref="loginForm" @submit.prevent="handleLogin" label-width="80px">
        <el-form-item label="口令" prop="password">
          <el-input 
            v-model="form.password" 
            style="width: 240px" 
            type="password" 
            placeholder="请输入管理员口令" 
            show-password 
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginAdmin } from '../../api/api.js';

export default {
  data() {
    return {
      form: {
        password: '',
      },
    };
  },
  methods: {
    async handleLogin() {
      try {
        // 登录请求
        const formData = new FormData();
        formData.append('password', this.form.password);
        const response = await loginAdmin(formData);

        if (response.status === 200) {
          this.$message.success('登录成功！');
          this.$router.push('/home'); // 跳转到主页
        }
      } catch (error) {
        const errorMessage =
          error.response?.data?.error || '登录失败，请稍后再试！';
        this.$message.error(errorMessage);
      }
    },
  },
};
</script>

<style scoped>
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.login-card {
  width: 400px;
  padding: 20px;
}
.el-button {
  margin-right: 20px;
}
</style>
